package com.xizi.single;

import java.lang.reflect.Constructor;

public enum EnumInstance {
    //枚举类的实例是final修饰的。不能继承其他类，也不可以被继承
    INSTANCE;

    public static EnumInstance getInstance() {
        return INSTANCE;
    }

    public static void main(String[] args) throws Exception {
        EnumInstance s = EnumInstance.getInstance();
        EnumInstance sual = EnumInstance.getInstance();
        Constructor<EnumInstance> constructor = EnumInstance.class.getDeclaredConstructor();
        constructor.setAccessible(true);
        EnumInstance s2 = constructor.newInstance();
        System.out.println(s + "\n" + sual + "\n" + s2);
        System.out.println("正常情况下，实例化两个实例是否相同：" + (s == sual));
        System.out.println("通过反射攻击单例模式情况下，实例化两个实例是否相同：" + (s == s2));
    }
}